package com.qf.zwy.office.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.office.common.base.utils.CommonBeanutils;
import com.qf.zwy.office.common.qo.PagingQueryQo;
import com.qf.zwy.office.common.vo.ApprovalFormVo;
import com.qf.zwy.office.entity.ApprovalForm;
import com.qf.zwy.office.mapper.ApprovalFormMapper;
import com.qf.zwy.office.service.ApprovalService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 子未语
 */
@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Resource
    private ApprovalFormMapper approvalFormMapper;

    @Override
    public IPage<ApprovalFormVo> pagingQuery(Integer page, Integer size, PagingQueryQo pagingQueryQo) {
        // 创建分页对象
        Page<ApprovalForm> approvalFormPage = new Page<>(page, size);
        QueryWrapper<ApprovalForm> wrapper = new QueryWrapper<>();
        // 构建查询条件
        wrapper.like(ApprovalForm.COL_TITLE, pagingQueryQo.getKey());
        if (!ObjectUtils.isEmpty(pagingQueryQo.getTheStartingTime())) {
            // 添加时间查询
            wrapper.between(ApprovalForm.COL_THE_STARTING_TIME, pagingQueryQo.getTheStartingTime(), new Date());
        }
        if (!ObjectUtils.isEmpty(pagingQueryQo.getApprovalType()) && pagingQueryQo.getApprovalType() != 0) {
            // 添加类型查询
            wrapper.eq(ApprovalForm.COL_APPROVAL_TYPE, pagingQueryQo.getApprovalType());
        }
        if (!ObjectUtils.isEmpty(pagingQueryQo.getApprovalStatus()) && pagingQueryQo.getApprovalStatus() != 0) {
            // 添加状态查询
            wrapper.eq(ApprovalForm.COL_APPROVAL_STATUS, pagingQueryQo.getApprovalStatus());
        }
        return approvalFormMapper.selectAll(approvalFormPage, wrapper);
    }

    @Override
    public ApprovalFormVo queryOne(Integer id) {
        QueryWrapper<ApprovalForm> qw = new QueryWrapper<>();
        qw.lambda().eq(ApprovalForm::getApprovalId, id);
        return CommonBeanutils.covertTo(approvalFormMapper.selectOne(qw), ApprovalFormVo::new);
    }


}
